package com.baide.sanguanheyi.luodao;

import com.baide.sanguanheyi.plc.PlcConfig;
import com.baide.sanguanheyi.sqlite.Content;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 洛道系统接口实现类
 */
@Service
public class LServiceImpl implements LService {

    private Logger logger = LoggerFactory.getLogger(LServiceImpl.class);

    @Resource
    Ldao dao;
    @Autowired
    PlcConfig config;

    @Override
    public Map<String, String> selectByBarcode(String barcode) {
        return dao.selectByBarcode(barcode);
    }

    @Override
    public void insert(Content content) {
        /**
         * @Tips: 适用于洛道系统数据插入
         * @param int type
         * @param Map<String, String> paramMap
         * @version v1.0.0
         * */
        // 上线扫描时要将过机状态记录到表中(有电子称,需要将称的重量ZL返回, HGBZ是当时过机的指令,无信息的也记录扫描动作)
        logger.info("过机状态记录到表");
        Map<String, String> insertXWight = new HashMap<>();
        insertXWight.put("bill_no", content.getBarCode());
//        insertXWight.put("user", cacheBean.getCt().get("UserId").toString());
        insertXWight.put("user", config.getXt_no());
        insertXWight.put("zl", content.getWeight());
        insertXWight.put("xt_no", config.getXt_no());
        insertXWight.put("hgbz", content.getCtOrder());
        insertXWight.put("trade_code", content.getLuoDao().get("trade_code"));
        dao.insertXWeight(insertXWight);
        // 图片表tb_pic的记录图片路径段
        logger.info("图片记录到表");
        Map<String, String> insertPic = new HashMap<>();
        insertPic.put("v_path", getPathFul(content.getCt().get("PRN").toString()));
        insertPic.put("bill_no", content.getBarCode());
        insertPic.put("d_pass_dateTime", content.getCt().get("JudgeTime").toString());
        insertPic.put("xt_no", config.getXt_no());
        dao.insertPic(insertPic);
        // 海关即决式布控,即点击CT机上嫌疑按钮时,需要记录当时的海关拍的动作HG,以及RESULTMASK人工判图或CT智能审图,以及CT返回的JUDGECATEGORY字段
        if (content.getCt().get("JudgeCategory").equals("0")) {
            logger.info("海关即决式布控记录到表");
            Map<String, String> insertHgOrgjOrder = new HashMap<>();
            insertHgOrgjOrder.put("bill_no", content.getBarCode());
            insertHgOrgjOrder.put("hg", "1");
            insertHgOrgjOrder.put("optUser", config.getXt_no());
            insertHgOrgjOrder.put("xt_no", config.getXt_no());
            insertHgOrgjOrder.put("trade_code", content.getLuoDao().get("trade_code"));
            insertHgOrgjOrder.put("return_mask", content.getCt().get("ResultMask").toString().equals("0") ? "1" : "0");
            insertHgOrgjOrder.put("judge_category", content.getCt().get("JudgeCategory").toString());
            dao.insertHgOrgjOrder(insertHgOrgjOrder);
        }
    }

    private String getPathFul(String prn) {
        try {
            String fileFoldName = prn.substring(10, 18);  //得到文件夹的名称
            // string pathFull = string.Format("\\pic/CT/{0}/{1}/{2}.jpg", machine_id, fileFoldName, prn);//@"\pic/CT/" + fileFoldName + "/" + prn + ".jpg";  //得到全路径
            return "\\pic/CT/" + config.getXt_no() + "/" + fileFoldName + "/" + prn + ".jpg";
        } catch (Exception e) {
            throw e;
        }
    }
}
